SPB Git forge

spb/spb-cloud

Public
10commits 1branches 0releases
3.2 MBsize
maindefault branch
yesterdaylast push
JavaScript 56.7% TypeScript 42.6%
1.5 KB · 50 lines typescript
Raw Blame History
1import { NextRequest, NextResponse } from "next/server";2import { prisma } from "@/lib/prisma";3import { loadFileData } from "@/lib/storage";45export async function GET(6  request: NextRequest,7  { params }: { params: { id: string } }8) {9  const file = await prisma.file.findUnique({ where: { id: params.id } });1011  if (!file) {12    return NextResponse.json({ error: "File not found" }, { status: 404 });13  }1415  // Contenu chiffré au repos → on déchiffre en mémoire puis on sert (avec support Range).16  const data = await loadFileData(file.storagePath, file.isEncrypted);17  const fileSize = data.length;1819  const range = request.headers.get("range");2021  if (range) {22    const parts = range.replace(/bytes=/, "").split("-");23    const start = parseInt(parts[0], 10);24    const end = parts[1]25      ? parseInt(parts[1], 10)26      : Math.min(start + 1024 * 1024 - 1, fileSize - 1);27    const chunk = data.subarray(start, end + 1);2829    return new NextResponse(new Uint8Array(chunk), {30      status: 206,31      headers: {32        "Content-Range": `bytes ${start}-${end}/${fileSize}`,33        "Accept-Ranges": "bytes",34        "Content-Length": String(chunk.length),35        "Content-Type": file.mimeType,36        "Cache-Control": "private, max-age=3600",37      },38    });39  }4041  return new NextResponse(new Uint8Array(data), {42    headers: {43      "Content-Length": String(fileSize),44      "Content-Type": file.mimeType,45      "Accept-Ranges": "bytes",46      "Cache-Control": "private, max-age=3600",47    },48  });49}50